import routes from './routes'
import {useRoutes} from 'react-router-dom'
import {getDynamicRouteChildren,getAllChildren} from '@/utils/commons'
import {useEffect,useState} from 'react'
import {getMenusApi} from '@/api/sys'
import useStorage from '@/hooks/useStorage'
export default function DynamicRouter() {
  const [childList,setChildList]=useState([])
  const [,,getToken]=useStorage('token')
  const getMenu=async()=>{
    const result=await getMenusApi()
    const cList=getAllChildren(result.data.menuList)
    setChildList(cList)
  }
  useEffect(()=>{
    const token=getToken()
    if(token){
      getMenu()
    }
  
  },[])
  //只有二级菜单所有数据获取到之后，才进行筛选工作
  if(childList.length>0){
    let newHomeChild=getDynamicRouteChildren(routes,childList)
    routes[2].children=newHomeChild
  }
  return useRoutes(routes)
    
}
